import request from "@/utils/request";
import {Loading} from 'element-ui';

let api = {
	/**
     * ���ظ���
     * @param url
     * @param name
     */
    downloadFile({url, name}) {
        if (url) {
            request.get(`${url}`, {
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
                    'appVersion': '1.0.0',
                    'framework': 'qb'
                },
                responseType: "blob",
            }).then(res => {
                let loading = Loading.service({
                    lock: true,
                    spinner: "el-icon-loading",
                    background: "rgba(0, 0, 0, 0.8)",
                    text: "加载中..."
                });
                this.download(res.data, name, loading);
            })
        }
    },
    /**
     * ������Ϊ����ģ���ṩ���������
     * @param data
     * @param filename
     */
    download: function (data, filename, loading) {
        let blob = new Blob([data], {
            type: "application/octet-stream"
        });
        if (typeof window.navigator.msSaveBlob !== "undefined") {
            window.navigator.msSaveBlob(blob, filename);
        } else {
            var blobURL = window.URL.createObjectURL(blob);
            var tempLink = document.createElement("a");
            tempLink.style.display = "none";
            tempLink.href = blobURL;
            tempLink.setAttribute("download", filename);
            if (typeof tempLink.download === "undefined") {
                tempLink.setAttribute("target", "_blank");
            }
            document.body.appendChild(tempLink);
            tempLink.click();
            document.body.removeChild(tempLink);
            window.URL.revokeObjectURL(blobURL);
        }
        loading && loading.close();
    }
};
export default api;